package com.hq.fxh.No5_User.MyModle;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;


import com.hq.fxh.No5_User.MyModle.Activity.item.BannerAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.BigAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.CardAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.GraphicAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.QQAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.QRCodeAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.StoreAdItem;
import com.hq.fxh.No5_User.MyModle.Activity.item.TiePianAdItem;

import java.util.Collections;
import java.util.List;


/**
 * @author 大圣
 * @ClassName AdTemplateAdapter
 * @Description : 广告模板适配器
 * @Created 2015/11/13 0013
 */
public class AdTemplateAdapter extends AdPagerAdapter {
    private List<AdBannerInfo> adInfos;
    private Context mContext;

    private OnClickListener onClickListener;

    public AdTemplateAdapter(Context context, List<AdBannerInfo> data) {
        super();
        mContext = context;
        adInfos = data;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup container) {
        final View view = convertView;
        final AdBannerInfo adInfo = adInfos.get(position);
        int type = adInfo.getAdvert_type_id();
        if (type == Config.AdTypeCode.AD_BANNER) {
            /** 通栏广告 */
            BannerAdItem bannerAdItem = null;
            if (view == null) {
                bannerAdItem = new BannerAdItem(mContext);
            } else {
                bannerAdItem = (BannerAdItem) view;
            }
            bannerAdItem.setInfo(adInfo);
            final BannerAdItem finalBannerAdItem = bannerAdItem;
                bannerAdItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int id = v.getId();
                        onClickListener.onClick(finalBannerAdItem, id, position);
                    }
                });

            return bannerAdItem;

        } else if (type == Config.AdTypeCode.AD_GRAPHIC) {
            /** 图文广告 */
            GraphicAdItem graphicAdItem = null;
            if (view == null) {
                graphicAdItem = new GraphicAdItem(mContext);
            } else {
                graphicAdItem = (GraphicAdItem) view;
            }
            graphicAdItem.setAdInfo(adInfo);
            final GraphicAdItem finalGraphicAdItem = graphicAdItem;
            graphicAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalGraphicAdItem, id, position);
                }
            });
            return graphicAdItem;

        } else if (type == Config.AdTypeCode.AD_CARD) {
            /** 名片广告 */
            CardAdItem cardAdItem = null;
            if (view == null) {
                cardAdItem = new CardAdItem(mContext);
            } else {
                cardAdItem = (CardAdItem) view;
            }
            cardAdItem.setAdInfo(adInfo);
            final CardAdItem finalCardAdItem = cardAdItem;
            cardAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalCardAdItem, id, position);
                }
            });
            return cardAdItem;
        } else if (type == Config.AdTypeCode.AD_QR_CODE) {
            /** 二维码广告 */
            QRCodeAdItem qrCodeAdItem = null;
            if (view == null) {
                qrCodeAdItem = new QRCodeAdItem(mContext);
            } else {
                qrCodeAdItem = (QRCodeAdItem) view;
            }
            qrCodeAdItem.setAdInfo(adInfo);
            final QRCodeAdItem finalQrCodeAdItem = qrCodeAdItem;
            qrCodeAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalQrCodeAdItem, id, position);
                }
            });
            return qrCodeAdItem;
        } else if (type == Config.AdTypeCode.AD_QQ) {
            /** QQ广告 */
            QQAdItem qqAdItem = null;
            if (view == null) {
                qqAdItem = new QQAdItem(mContext);
            } else {
                qqAdItem = (QQAdItem) view;
            }
            qqAdItem.setAdInfo(adInfo);
            final QQAdItem finalQQAdItem = qqAdItem;
            qqAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalQQAdItem, id, position);
                }
            });
            return qqAdItem;
        } else if (type == Config.AdTypeCode.AD_Store) {
            /** 店铺广告 */
            StoreAdItem storeAdItem = null;
            if (view == null) {
                storeAdItem = new StoreAdItem(mContext);
            } else {
                storeAdItem = (StoreAdItem) view;
            }
            storeAdItem.setAdInfo(adInfo);
            final StoreAdItem finalGraphicAdItem = storeAdItem;
            storeAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalGraphicAdItem, id, position);
                }
            });
            return storeAdItem;

        }else if (type == Config.AdTypeCode.AD_TiePian) {
            /** 贴片广告 */
            TiePianAdItem tiePianAdItem = null;
            if (view == null) {
                tiePianAdItem = new TiePianAdItem(mContext);
            } else {
                tiePianAdItem = (TiePianAdItem) view;
            }
            tiePianAdItem.setInfo(adInfo);
            final TiePianAdItem finalTiePianAdItem = tiePianAdItem;
            tiePianAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalTiePianAdItem, id, position);
                }
            });
            return tiePianAdItem;

        } else if (type == Config.AdTypeCode.AD_BIG_BANNER) {
            /** 大图通栏广告 */
            BigAdItem bigAdItem = null;
            if (view == null) {
                bigAdItem = new BigAdItem(mContext);
            } else {
                bigAdItem = (BigAdItem) view;
            }
            bigAdItem.setInfo(adInfo);
            final BigAdItem finalbigAdItemAdItem = bigAdItem;
            bigAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = v.getId();
                    onClickListener.onClick(finalbigAdItemAdItem, id, position);
                }
            });
            return bigAdItem;
        }
        else {
            BannerAdItem bannerAdItem = null;
            if (view == null) {
                bannerAdItem = new BannerAdItem(mContext);
            } else {
                bannerAdItem = (BannerAdItem) view;
            }
//            bannerAdItem.setInfo(adInfo);
            final BannerAdItem finalBannerAdItem = bannerAdItem;
            bannerAdItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int id = v.getId();
                    onClickListener.onClick(finalBannerAdItem, id, position);
                }
            });
            return bannerAdItem;
        }

    }

    public void setOnClickListener(OnClickListener listener) {
        onClickListener = listener;
    }

    @Override
    public int getCount() {
        return adInfos.size();
    }

//    @Override
//    public int getItemViewType(int position) {
//        AdInfo adInfo = getItem(position);
//        return adInfo != null?adInfo.getAdType() - 1:0;
//    }


//    @Override
//    public int getViewTypeCount() {
//      return 4;
//    }

    public void add(AdBannerInfo obj, int positon) {
        if (obj == null)
            return;
        adInfos.add(positon, obj);
        notifyDataSetChanged();
    }

    public void add(AdBannerInfo obj) {
        if (obj == null)
            return;
        adInfos.add(obj);
        notifyDataSetChanged();
    }

    public void addAll(int position,List<AdBannerInfo> data) {
        if (data == null || data.isEmpty())
            return;
        adInfos.addAll(position,data);
        notifyDataSetChanged();
    }
    public void addAll(List<AdBannerInfo> data) {
        if (data == null || data.isEmpty())
            return;
        adInfos.addAll(data);
        notifyDataSetChanged();
    }

    public void addAll(AdBannerInfo[] data) {
        if (data == null || data.length == 0)
            return;
        Collections.addAll(adInfos, data);
        notifyDataSetChanged();
    }

    public void clear() {
        adInfos.clear();
        notifyDataSetChanged();
    }

    public void remove(int position) {
        adInfos.remove(position);
        notifyDataSetChanged();
    }

    public void remove(AdInfo obj) {
        int index = adInfos.indexOf(obj);
        adInfos.remove(index);
        notifyDataSetChanged();
    }

    public int getPosition(AdInfo item) {
        return adInfos.indexOf(item);
    }


    public AdBannerInfo getItem(int position) {
        if (position <= adInfos.size()) {
            return adInfos.get(position);
        } else {
            return null;
        }
    }

    public List<AdBannerInfo> getList() {
        return adInfos;
    }

    public interface OnClickListener {
        void onClick(View v, int id, int position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
